#ifndef TIMEMEASUREMENT_H
#define TIMEMEASUREMENT_H

#include <iostream>

#ifdef _WIN32
# include <windows.h>
#else
# ifdef __unix__
#  include <sys/time.h>
# else
#  include <sys/time.h>
# endif
#endif

/*!
 * \brief The timer struct contain method to perform cross platform time measurements.
 */
class timer
{

#ifdef _WIN32
    FILETIME ttmp={0,0};
    ULARGE_INTEGER tbegin,tend;
#else
# ifdef __unix__
    struct timeval tbegin,tend;
# else
    time_t tbegin;
# endif
#endif

public:
    /*! \brief start the timer */
    void start() {
#ifdef _WIN32
        ::GetSystemTimeAsFileTime(&ttmp);
        tbegin.HighPart=ttmp.dwHighDateTime;
        tbegin.LowPart=ttmp.dwLowDateTime;
#else
# ifdef __unix__
        gettimeofday(&tbegin,NULL);
# else
        tbegin=time(NULL);
# endif
#endif
    }

    /*! \brief stop the timer and return the time elapsed between previous start() call */
    double stop() {
#ifdef _WIN32
        ::GetSystemTimeAsFileTime(&ttmp);
        tend.HighPart=ttmp.dwHighDateTime;
        tend.LowPart=ttmp.dwLowDateTime;
        return ((double)((tend.QuadPart-tbegin.QuadPart)/10000))/1000.;
#else
# ifdef __unix__
        gettimeofday(&tend,NULL);
        return ((double)(1000*(tend.tv_sec-tbegin.tv_sec)+((tend.tv_usec-tbegin.tv_usec)/1000)))/1000.;
# else
        return difftime(time(NULL),tbegin);
# endif
#endif
    }

};

#endif // TIMEMEASUREMENT_H
